<template>
  <MyDialog
    v-model="isVisible"
    :type="type"
    class="audit-dialog-wrapper"
    width="1200"
    :dialog-title="dialogTitle"
    :need-modal="needModal"
    @closed="closed"
  >
    <my-form
      ref="ruleForm"
      v-model="dataForm"
      label-width="120px"
      :disabled="currentRow.type === 'detail'"
      :model="dataForm"
      :rules="rules"
      :is-grid="false"
      :items-datas="state.itemsDatas"
    >
    </my-form>
    <template #footer>
      <my-button type="reset" title="关闭" @click="closed" />
      <my-button v-if="currentRow.type !== 'detail'" type="save" title="保存" :loading="buttonLoading" @click="submitForm" />
    </template>
  </MyDialog>
</template>

<script setup lang="ts" name="addAb01">
import { ref } from 'vue';
import { addAb02, getAb02, updateAb02 } from '@/api/yuanshidian/ab02';
// import { addAb03, getAb03, updateAb03 } from '@/api/yuanshidian/ab03';
const { proxy } = getCurrentInstance();
const props = defineProps(
  {
    dialogTitle: {
      type: String,
      default: '弹窗标题'
    },
    needModal: {
      type: Boolean,
      default: true
    },
    modelValue: {
      type: Boolean,
      default: false
    },
    currentRow: {
      type: Object,
      default: () => {}
    }
  },
  ['modelValue']
);
const buttonLoading = ref(false);
const ruleForm = ref<ElFormInstance>();
const reg = ref(
  /^\d{6}((((((19|20)\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(((19|20)\d{2})(0[13578]|1[02])31)|((19|20)\d{2})02(0[1-9]|1\d|2[0-8])|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1[02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))(\d|X|x)$/
);
const validateCode = (rule, value, callback) => {
  console.log(rule);
  console.log(value);
  if (!reg.value.test(value)) {
    callback(new Error('请输入正确的证件号码'));
  } else {
    callback();
  }
};
const state = reactive({
  rules: {
    gr04: [{ required: true, message: '请输入', trigger: 'blur' }],
    gr02: [
      { required: true, message: '请输入', trigger: 'blur' },
      { validator: validateCode, trigger: 'blur' }
    ],
    // gr01: [{ required: true, message: '请输入', trigger: 'blur' }],
    gr07: [{ required: true, message: '请选择', trigger: 'blur' }],
    gr08: [{ required: true, message: '请选择', trigger: 'blur' }],
    gr09: [{ required: true, message: '请输入', trigger: 'blur' }]
  },
  dataForm: {
    id: '',
    aaa027: '',
    grbh: '',
    gr01: '',
    gr02: '',
    gr03: '',
    gr04: '',
    gr05: '',
    gr06: '',
    gr07: '',
    gr08: '',
    gr09: '',
    gr10: '',
    gr11b: '',
    gr11a: '',
    gr11: '',
    gr12: '',
    gr13: '',
    gr14: '',
    gr15: '',
    gr16: '',
    gr17: '',
    gr18: '',
    gr19: '',
    gr20: '',
    gr21: '',
    gr22: '',
    gr23: '',
    yd03: '',
    yd08: '',
    yd10: '',
    dw60: '',
    dh2: '',
    dw17: '',
    dw61: '',
    dh3: '',
    gr26: '',
    gr50: '',
    gr51: '',
    gr52: '',
    gr53: '',
    gr60: '',
    systime: '',
    tx52: '',
    tx57: '',
    ryType: '',
    zrStartmonth: '',
    refStatus: '',
    zrAmount: null,
    zrEndmonth: '',
    tx58: '',
    yd11: '',
    yd12: null,
    gr24: null
  },
  itemsDatas: [
    { label: '统筹区编码', prop: 'aaa027', type: 'input' },
    { label: '原编号', prop: 'grbh', type: 'input' },
    { label: '个人编号', prop: 'gr01', type: 'input' },
    { label: '身份证号码', prop: 'gr02', type: 'input' },
    { label: '个人账号', prop: 'gr03', type: 'input' },
    { label: '职工姓名', prop: 'gr04', type: 'input' },
    { label: '性别', prop: 'gr05', type: 'input' },
    { label: '民族', prop: 'gr06', type: 'input' },
    { label: '出生日期', prop: 'gr07', type: 'date', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
    { label: '工作时间', prop: 'gr08', type: 'date', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
    {
      label: '职工身份',
      prop: 'gr09',
      type: 'select',
      options: [
        { value: '1', label: '固定工' },
        { value: '2', label: '合同工' }
      ]
    },
    {
      label: '人员类型',
      prop: 'ryType',
      type: 'select',
      options: [
        { value: 1, label: '在职人员' },
        { value: 2, label: '离退人员' },
        { value: 3, label: '异动人员' }
      ]
    },
    {
      label: '退费标识',
      prop: 'refStatus',
      type: 'select',
      options: [
        { value: 0, label: '未退费' },
        { value: 1, label: '已退费' }
      ]
    },
    { label: '转入金额', prop: 'zrAmount', type: 'number', min: 0, controls: false },
    { label: '转入开始月', prop: 'zrStartmonth', type: 'month', valueFormat: 'YYYY-MM' },
    { label: '转入截至月', prop: 'zrEndmonth', type: 'month', valueFormat: 'YYYY-MM' },
    { label: '建立账户时间', prop: 'gr10', type: 'date', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
    { label: '计算值', prop: 'gr11a', type: 'number', min: 0, controls: false },
    { label: '调整值', prop: 'gr11b', type: 'number', min: 0, controls: false },
    { label: '视同', prop: 'gr11', type: 'input' },
    { label: '统筹开始时间', prop: 'gr12', type: 'date', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
    { label: '统筹结束时间', prop: 'gr13', type: 'date', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
    { label: '统筹缴费月', prop: 'gr14', type: 'number', min: 0, controls: false },
    { label: '统筹缴费金额', prop: 'gr15', type: 'number', min: 0, controls: false },
    { label: '统筹单位缴费额', prop: 'gr16', type: 'number', min: 0, controls: false },
    { label: '统筹个人缴费额', prop: 'gr17', type: 'number', min: 0, controls: false },
    { label: '实际工资', prop: 'gr18', type: 'number', min: 0, controls: false },
    { label: '缴费基数', prop: 'gr19', type: 'number', min: 0, controls: false },
    { label: '个人缴费额', prop: 'gr20', type: 'number', min: 0, controls: false },
    { label: '94调标工资', prop: 'gr21', type: 'number', min: 0, controls: false },
    { label: '状态', prop: 'gr22', type: 'input' },
    { label: '异动原因', prop: 'gr23', type: 'input' },
    { label: '异动月份', prop: 'yd03', type: 'input' },
    { label: '记账月份', prop: 'yd08', type: 'input' },
    { label: '异动时补(退)额', prop: 'yd10', type: 'number', min: 0, controls: false },
    { label: '补(退)标志', prop: 'yd11', type: 'input' },
    { label: '其中单位补(退)', prop: 'yd12', type: 'number', min: 0, controls: false },
    { label: '新工资', prop: 'gr24', type: 'number', min: 0, controls: false },
    { label: '调出单位编码', prop: 'gr26', type: 'input' },
    { label: '补充额标准', prop: 'gr50', type: 'number', min: 0, controls: false },
    { label: '储蓄额标准', prop: 'gr51', type: 'number', min: 0, controls: false },
    { label: '本次补充额', prop: 'gr52', type: 'number', min: 0, controls: false },
    { label: '本次储蓄额', prop: 'gr53', type: 'number', min: 0, controls: false },
    { label: '折算工龄', prop: 'gr60', type: 'input', min: 0, controls: false },
    { label: '系统时间', prop: 'systime', type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
    { label: '通讯地址', prop: 'tx52', type: 'input' },
    { label: '联系电话', prop: 'tx57', type: 'input' },
    { label: '邮政编码', prop: 'tx58', type: 'input' }
  ]
});
const { dataForm, rules } = toRefs(state);
const isVisible = ref(false);
watch(
  () => props.modelValue,
  async (v) => {
    isVisible.value = v;
    if (props.currentRow.id && v) {
      var res = {};
      res = await getAb02(props.currentRow.id);
      Object.keys(dataForm.value).forEach((item) => {
        dataForm.value[item] = res.data[item];
      });
      console.log(dataForm.value);
    }
  },
  { immediate: true, deep: true }
);
const emit = defineEmits(['update:modelValue', 'iniSearch']);
const closed = () => {
  emit('update:modelValue');
};
/** 表单重置 */
const reset = () => {
  ruleForm.value?.resetFields();
};

/** 提交按钮 */
const submitForm = () => {
  ruleForm.value?.validate(async (valid) => {
    if (valid) {
      var params = {
        ...dataForm.value
      };
      params.gr01 = params.gr01 ? params.gr01 : params.gr02;
      buttonLoading.value = true;
      if (dataForm.value.id) {
        await updateAb02(params).finally(() => (buttonLoading.value = false));
      } else {
        await addAb02(params).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      reset();
      emit('iniSearch');
      closed();
    }
  });
};
</script>
